package com.lmz.seckill.scheduled;

import com.lmz.seckill.service.SeckillService;
import lombok.extern.slf4j.Slf4j;
import org.redisson.api.RLock;
import org.redisson.api.RedissonClient;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import java.util.concurrent.TimeUnit;

/**
 * 秒杀商品定时上架
 * --每天晚上3点上架最近三天需要秒杀的商品
 */
@Slf4j
@Component
public class SeckillSkuScheduled {

    private final String upload_lock = "seckill:upload:lock";

    @Autowired
    private SeckillService seckillService;
    @Autowired
    private RedissonClient redissonClient;

    //
    //// TODO 幂等性，不需要重复上架
    //@Scheduled(cron = "*/3 * * * * ?")
    //public void uploadSeckillSku() {
    //    log.info("上架");
    //    RLock lock = redissonClient.getLock(upload_lock);
    //    lock.lock(10, TimeUnit.SECONDS);
    //    try {
    //        seckillService.uploadSeckillSku();
    //    } finally {
    //        lock.unlock();
    //    }
    //}

}